﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DNA.Mvc.Services;
using DNA.OpenAPI.Atom;

namespace DNA.Mvc.Syndication
{
    public class WebPageAtomEntriesGenerator : AtomEntriesGeneratorBase
    {
        private WebSiteContext context;
        public WebPageAtomEntriesGenerator(WebSiteContext webContext) { context = webContext; }

        protected override IEnumerable<AtomEntry> OnCreateAtomEntries(IDictionary<string, object> data)
        {
            var pages = context.Web.WebPages.Where(p => p.AllowAnonymous).OrderBy(p => p.ID);
            var entries = new List<AtomEntry>();
            foreach (var page in pages)
            {
                if (!page.ShowInMenu)
                    continue;

                entries.Add(new AtomEntry()
                {
                    ID = UrlResolver.Resolve(context.HttpContext, page),
                    Author = new AtomPersonConstruct()
                    {
                        Name = page.Owner
                    },
                    Content = new AtomContent()
                    {
                        Text = !string.IsNullOrEmpty(page.Description) ? page.Description : page.Title,
                        ContentType = "text/html",
                        SourceUrl = UrlResolver.Resolve(context.HttpContext, page)
                    },
                    Summary = page.Description,
                    Title = page.Title,
                    Published = page.Created,
                    Updated = page.LastModified
                });
            }
            return entries;
        }
    }
}